home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / sh-utils.12 / sh-utils / sh-utils-1.12 / src / nice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  4.7 KB  |  207 lines

  1. /* nice -- run a program with modified scheduling priority
  2.    Copyright (C) 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* David MacKenzie <djm@gnu.ai.mit.edu> */
  19.  
  20. #include <config.h>
  21. #include <stdio.h>
  22.  
  23. #define NDEBUG
  24. #include <assert.h>
  25.  
  26. #include <getopt.h>
  27. #include <sys/types.h>
  28. #ifndef NICE_PRIORITY
  29. #include <sys/time.h>
  30. #include <sys/resource.h>
  31. #endif
  32.  
  33. #include "version.h"
  34. #include "system.h"
  35. #include "long-options.h"
  36.  
  37. #ifdef NICE_PRIORITY
  38. #define GET_PRIORITY() nice (0)
  39. #else
  40. #define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
  41. #endif
  42.  
  43. void error ();
  44.  
  45. static int isinteger ();
  46. static void usage ();
  47.  
  48. /* The name this program was run with. */
  49. char *program_name;
  50.  
  51. static struct option const longopts[] =
  52. {
  53.   {"adjustment", required_argument, NULL, 'n'},
  54.   {NULL, 0, NULL, 0}
  55. };
  56.  
  57. void
  58. main (argc, argv)
  59.      int argc;
  60.      char **argv;
  61. {
  62.   int current_priority;
  63.   int adjustment = 0;
  64.   int minusflag = 0;
  65.   int adjustment_given = 0;
  66.   int long_option_priority = 0;
  67.   int last_optind = 0;
  68.  
  69.   program_name = argv[0];
  70.   parse_long_options (argc, argv, "nice", version_string, usage);
  71.  
  72.   for (optind = 1; optind < argc; /* empty */)
  73.     {
  74.       char *s;
  75.  
  76.       s = argv[optind];
  77.  
  78.       if (s[0] == '-' && s[1] == '-' && ISDIGIT (s[2]))
  79.     {
  80.       if (!isinteger (&s[2]))
  81.         error (1, 0, "invalid option `%s'", s);
  82.  
  83.       minusflag = 1;
  84.       adjustment = atoi (&s[2]);
  85.       adjustment_given = 1;
  86.       long_option_priority = 1;
  87.       ++optind;
  88.     }
  89.       else
  90.     {
  91.       int optc;
  92.       while ((optc = getopt_long (argc, argv, "+0123456789n:", longopts,
  93.                       (int *) 0)) != EOF)
  94.         {
  95.           switch (optc)
  96.         {
  97.         case '?':
  98.           usage (1);
  99.  
  100.         case 'n':
  101.           if (!isinteger (optarg))
  102.             error (1, 0, "invalid priority `%s'", optarg);
  103.           adjustment = atoi (optarg);
  104.           adjustment_given = 1;
  105.           break;
  106.  
  107.         default:
  108.           assert (ISDIGIT (optc));
  109.           /* Reset ADJUSTMENT if the last priority-specifying option
  110.              was not of the same type or if it was, but a separate
  111.              option.  */
  112.           if (long_option_priority ||
  113.               (adjustment_given && optind != last_optind))
  114.             {
  115.               long_option_priority = 0;
  116.               adjustment = 0;
  117.             }
  118.           adjustment = adjustment * 10 + optc - '0';
  119.           adjustment_given = 1;
  120.           last_optind = optind;
  121.         }
  122.         }
  123.       if (optc == EOF)
  124.         break;
  125.     }
  126.     }
  127.  
  128.   if (minusflag)
  129.     adjustment = -adjustment;
  130.   if (!adjustment_given)
  131.     adjustment = 10;
  132.  
  133.   if (optind == argc)
  134.     {
  135.       if (adjustment_given)
  136.     {
  137.       error (0, 0, "a command must be given with an adjustment");
  138.       usage (1);
  139.     }
  140.       /* No command given; print the priority. */
  141.       errno = 0;
  142.       current_priority = GET_PRIORITY ();
  143.       if (current_priority == -1 && errno != 0)
  144.     error (1, errno, "cannot get priority");
  145.       printf ("%d\n", current_priority);
  146.       exit (0);
  147.     }
  148.  
  149. #ifndef NICE_PRIORITY
  150.   errno = 0;
  151.   current_priority = GET_PRIORITY ();
  152.   if (current_priority == -1 && errno != 0)
  153.     error (1, errno, "cannot get priority");
  154.   if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
  155. #else
  156.   if (nice (adjustment) == -1)
  157. #endif
  158.     error (1, errno, "cannot set priority");
  159.  
  160.   execvp (argv[optind], &argv[optind]);
  161.   error (errno == ENOENT ? 127 : 126, errno, "%s", argv[optind]);
  162. }
  163.  
  164. /* Return nonzero if S represents a (possibly signed) decimal integer,
  165.    zero if not. */
  166.  
  167. static int
  168. isinteger (s)
  169.      char *s;
  170. {
  171.   if (*s == '-' || *s == '+')
  172.     ++s;
  173.   if (*s == 0)
  174.     return 0;
  175.   while (*s)
  176.     {
  177.       if (!ISDIGIT (*s))
  178.     return 0;
  179.       ++s;
  180.     }
  181.   return 1;
  182. }
  183.  
  184. static void
  185. usage (status)
  186.      int status;
  187. {
  188.   if (status != 0)
  189.     fprintf (stderr, "Try `%s --help' for more information.\n",
  190.          program_name);
  191.   else
  192.     {
  193.       printf ("Usage: %s [OPTION]... [COMMAND [ARG]...]\n", program_name);
  194.       printf ("\
  195. \n\
  196.   -ADJUST                   increment priority by ADJUST first\n\
  197.   -n, --adjustment=ADJUST   same as -ADJUST\n\
  198.       --help                display this help and exit\n\
  199.       --version             output version information and exit\n\
  200. \n\
  201. With no COMMAND, print the current scheduling priority.  ADJUST is 10\n\
  202. by default.  Range goes from -20 (highest priority) to 19 (lowest).\n\
  203. ");
  204.     }
  205.   exit (status);
  206. }
  207.